﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Kooboo.CMS.Form;
using Kooboo.CMS.Form.Html;
using Kooboo.Globalization;

namespace Kooboo.CMS.Toolkit.Controls
{
    public class MediaFile : Kooboo.CMS.Form.Html.Controls.Input
    {
        public override string Type
        {
            get { return "hidden"; }
        }

        private string _name;
        public override string Name
        {
            get
            {
                if (string.IsNullOrEmpty(_name))
                {
                    _name = this.GetType().Name;
                }

                return _name;
            }
        }

        /// <summary>
        /// false
        /// </summary>
        protected virtual bool AllowMultipleFiles
        {
            get
            {
                return false;
            }
        }

        /// <summary>
        /// /^.+\..+$/
        /// </summary>
        protected virtual string Validation
        {
            get
            {
                return @"/^.+\..+$/";
            }
        }

        /// <summary>
        /// Please select a valid file
        /// </summary>
        protected virtual string ValidationErrorMessage
        {
            get
            {
                return "Please select a valid file";
            }
        }

        protected override string RenderInput(Kooboo.CMS.Form.IColumn column)
        {
            string input = base.RenderInput(column);
            string script = "<script src=\"@Kooboo.CMS.Toolkit.Controls.ControlsScript.GetWebResourceUrl()\" type=\"text/javascript\" ></script>";
            string options = @"{
                allowMultipleFiles: " + AllowMultipleFiles.ToString().ToLower() + @",
                validation: " + Validation + @",
                validationErrorMessage: '" + ValidationErrorMessage + @"'
            }";

            // <%:Url.Action("Selection", "MediaContent", ViewContext.RequestContext.AllRouteValues())%>
            /*string mediaLibraryUrl = @"<%:Url.Action(""Selection"", ""MediaContent"", new {
                        repositoryName = Kooboo.CMS.Sites.Models.Site.Current.Repository,
                        siteName = Kooboo.CMS.Sites.Models.Site.Current.Name
                    })%>";*/
            string mediaLibraryUrl = @"@Url.Action(""Selection"", ""MediaContent"", new {
                        repositoryName = Kooboo.CMS.Sites.Models.Site.Current.Repository,
                        siteName = Kooboo.CMS.Sites.Models.Site.Current.FullName
                    })";

            input += String.Format(@"
                {0}
                <a id='addMedia_{1}' columnName='{1}' 
                    href='{2}' class='o-icon add'>add</a>
                <script type='text/javascript'>
                    $('#addMedia_{1}').mediaControl({3});
                </script>
            ", script, column.Name, mediaLibraryUrl, options);
            return input;
        }
    }
}